fix(llm): improve graph JSON parsing robustness for LLM outputs#332
Merged
imbajin merged 4 commits intoMay 19, 2026
Merged
Conversation
…tputs
Different LLMs return graph extraction results in varying formats:
- Some wrap JSON in markdown code blocks (```json ... ```)
- Some return a flat array of vertices/edges instead of a structured object
This causes json.JSONDecodeError when the greedy regex ({.*}) captures
invalid content from markdown-wrapped or array-formatted responses.
Changes:
- Strip markdown code blocks before JSON extraction
- Support both object ({...}) and array ([...]) JSON formats
- Auto-convert flat arrays to {"vertices": [...], "edges": [...]} format
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- cover markdown fenced property graph JSON output - cover flat array vertex and edge parsing - keep tests scoped to _extract_and_filter_label behavior
- add coverage for fenced JSON with prose around it - verify flat arrays drop invalid graph items - cover malformed fenced JSON returning no graph items
imbajin
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_extract_and_filter_labelto handle varying LLM output formats{"vertices":[...], "edges":[...]}(object) and flat array formatsProblem
When using reasoning models (e.g., DeepSeek V4) for graph extraction, the LLM may return:
\``json ... ```), which breaks the greedy regex({.*})`[vertex, edge, ...]instead of the expected object{"vertices": [...], "edges": [...]}Both cases cause
json.JSONDecodeErrorand result in empty extraction output even though the LLM correctly identified entities and relationships.Solution
\``json/````) before regex matching{...}) and arrays ([...])typefield intoverticesandedgesTest plan
🤖 Generated with Claude Code